﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using chihuopig.Web.Framework;
using Microsoft.AspNetCore.Http;
using chihuopig.IService;
using chihuopig.Web.verfiy;
using chihuopig.Tool;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace chihuopig.Web.Admin.Controllers
{
    public class AdminArticleController : AdminWattantyController
    {
        IHttpContextAccessor _iHttpContextAccessor;
        IArticleService _articleservice;
        public AdminArticleController 
         (
              IHttpContextAccessor iHttpContextAccessor,
              IArticleService iarticleservice
          ) :base(iHttpContextAccessor) 
        {
            _iHttpContextAccessor = iHttpContextAccessor;
            _articleservice = iarticleservice;
        }

        public async Task<IActionResult> List(int Pageindex = 1, int PageSize = 10)
        {
            var list = await _articleservice.GetPage(Pageindex, PageSize);
            var total = await _articleservice.GetPageTotal(PageSize);
            ViewBag.Pageindex = Pageindex;
         
            ViewBag.Start = Pageindex + 5 > total ? 1 : Pageindex + 5;
            ViewBag.GetPageTotal = total + 5 > total ? total : total + 5;
            return View(list);
        }

        public async Task<IActionResult> Add()
        {
            
            var model= _articleservice.GetArticle(0);
            return View(await model);
        }

        public async Task<IActionResult> Edit(long id)
        {
            var model =await _articleservice.GetArticle(id);
            ViewBag.Id = id;
            ViewBag.Html = model.article.Page;
            ViewBag.HtmlContent = await UtilityHelper.FindHtmlContent(System.Environment.CurrentDirectory + "\\ContentHtml\\" + model.article.ContentHtml);
            return View(model);
        }
        [HttpPost]
        public async Task<IActionResult> Edit(ArticleWriterVerfiy model)
        {
            if (ModelState.IsValid)
            {
                
                string Temppath = System.Environment.CurrentDirectory + "\\Htmltemplate\\htmlpage.cshtml";
                string SavePath = System.Environment.CurrentDirectory + "\\Web\\Views\\Home\\" + model.Html;
                string SaveShtml = UtilityHelper.SaveShtml(Temppath, model.Centonet, SavePath, model.Html);
                string SvePath = System.Environment.CurrentDirectory + "\\ContentHtml\\" + model.Html;
                string Contenthtml = UtilityHelper.SaveShtml(SvePath, model.Html, model.Centonet);
                Model.Article mr = new Model.Article();
                mr.Id = model.Id;
                mr.Imgsrc = model.Imgsrc;
                if (model.Imgsrc.Contains("data"))
                {
                    mr.Imgsrc =UtilityHelper.SaveBase64Img(model.Imgsrc);
                }
                mr.IsDelete = false;
                mr.Page = model.Html;
                mr.ContentHtml= model.Html;
                mr.Addtime = DateTime.Now;
                mr.WriterId = model.WriterId;
                mr.Title = model.Title;
                var RestInt = _articleservice.Updateable(mr);
                if (await RestInt > 0)
                {
                    return Json(new JsonMsg() { msg = "修改成功", succeed = true });
                }
            }
           return Json(ModelStateVefify.Vefifymsg(ModelState));
        }



        [HttpPost]
        public async Task<IActionResult> Add(ArticleWriterVerfiy model)
        {
            if(ModelState.IsValid)
            { 
                string Imgsrc=UtilityHelper.SaveBase64Img(model.Imgsrc);
                string Temppath = System.Environment.CurrentDirectory + "\\Htmltemplate\\htmlpage.cshtml";
                string FielName = DateTime.Now.ToString("yyyymmddhhmmss") + ".shtml";
                string SavePath = System.Environment.CurrentDirectory + "\\Web\\Views\\Home\\"+ FielName;
                string SaveShtml= UtilityHelper.SaveShtml(Temppath, model.Centonet, SavePath, FielName);
                string SvePath = System.Environment.CurrentDirectory + "\\ContentHtml\\" + FielName;
                string Contenthtml=UtilityHelper.SaveShtml(SvePath, FielName, model.Centonet);
                var RestInt=_articleservice.Insertable(new Model.Article() { Imgsrc= Imgsrc, Addtime=DateTime.Now, browseNum=0, IsDelete=false, Page= SaveShtml, Title=model.Title, WriterId=model.WriterId, ContentHtml= Contenthtml });
                if (await RestInt > 0)
                {
                    return Json(new JsonMsg() { msg = "添加成功", succeed = true });
                }
            }
            return Json(ModelStateVefify.Vefifymsg(ModelState));
        }

        [HttpPost]
        public async Task<IActionResult> DelAll(long[] id)
        {
            var i= _articleservice.delAll(id);
            if (await i>0)
            {
                return Json(new JsonMsg() { msg="删除成功",succeed=true });
            }
           return Json(new JsonMsg() { msg = "删除失败", succeed = false });
        }
       
      
    }
}